package com.cn.springcloudwechat.controller.shop;

import com.alibaba.fastjson.JSONObject;
import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.common.utils.base.Const;
import com.cn.springcloudwechat.controller.shop.WeiXinPay;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.YmUserPoint;
import com.cn.springcloudwechat.service.pay.PayService;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserPointService;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wuyanzu
 * @Date: 2018/8/21
 */
@RequestMapping("/web/shop/")
@RestController
public class GetLoginUser {

    @Autowired
    YmUserService ymUserService;

    @Autowired
    YmUserPointService ymUserPointService;

    @Autowired
    PayService payService;

    @RequestMapping("getUser")
    public YmUser getUser() {
        return ThreadLocalUtil.getUser();
    }

    @RequestMapping("getTestUser")
    public YmUser getTestUser() {

        System.out.println("123");
        return ymUserService.getUserByOpenId("oTO_rv94yD5n5X89amPfw_VrrCPs");
    }

    @RequestMapping("getPoint")
    public Long getPoint(Long userId) {
        System.err.println(userId);
        YmUserPoint ymUserPoint = ymUserPointService.getNewestPoint(userId);
        return ymUserPoint.getTotalPoint();
    }

    @RequestMapping("getTestPoint")
    public Long getPointByUserId() {
        YmUserPoint ymUserPoint = ymUserPointService.getNewestPoint(getTestUser().getUserId());
        Long total = ymUserPoint.getTotalPoint();
        return total;
    }

    @RequestMapping("testInsertUserPoint")
    public Integer insertUserPoint(Long userId,String changeDes,Integer changeType,String change) {
        return ymUserPointService.insertUserPoint(userId, changeDes, changeType, change);
    }

    @RequestMapping("wechatPay")
    public AjaxRes pay(AjaxRes ajaxRes, HttpServletRequest request, Long orderId, String ymUser,Long totalcost) {
        if (orderId != null) {
            YmUser user = (YmUser) JSONObject.parseObject(ymUser, YmUser.class);
            System.err.println(user.getUserId()+"==============");
            if (user != null) {
                String body = "购买商品";
                Map<Object, Object> map = this.pay(request, totalcost.intValue(), body, orderId, user.getRegisterChannelId());
                for (Map.Entry<Object, Object> m : map.entrySet()) {
                    System.err.println(m.getValue());
                }
                if (map != null) {
                    ajaxRes.setSucceed(map);
                } else {
                    ajaxRes.setFailMsg("支付失败");
                }
            } else {
                ajaxRes.setFailMsg("用户信息有误");
            }
        }
        return ajaxRes;
    }

    @RequestMapping("pay")
    public Map<Object, Object> pay(HttpServletRequest request, Integer totalFee, String body,Long orderId,String openId) {
        Map<Object,Object> data= new HashMap<>();
        try {
            //String openid = ThreadLocalUtil.getUser().getRegisterChannelId();
            //String openid = "oTO_rv7Pae08jI7_eVWXAoJqyv4g";
            if(openId==null){
                throw new IllegalArgumentException("openId为空");
            }
            WeiXinPay weiXinPay = new WeiXinPay();
            System.err.println(body);
            data = weiXinPay.pay(body,totalFee,openId,String.valueOf(orderId));
            for (Map.Entry<Object, Object> m: data.entrySet()) {
                System.err.println(m.getValue());
            }
            System.out.println("微信支付:"+data);
            return data;
        }catch (Exception e){
            System.err.println(e.getMessage());
        }
        return null;
    }
}
